package org.bovver.service;

import org.bovver.model.Product;
import org.bovver.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Paul on 2014/11/26.
 *
 */
@Service
public class ProductService {
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    private RowMapper<Product> productRowMapper = new RowMapper<Product>() {
        @Override
        public Product mapRow(ResultSet rs, int i) throws SQLException {
            Product product = new Product();
            product.setId(rs.getLong("id"));
            product.setCreateDate(rs.getDate("create_date"));
            product.setModifyDate(rs.getDate("modify_date"));
            product.setAllocatedStock(rs.getInt("allocated_stock"));
            product.setAttributeValue0(rs.getString("attribute_value0"));
            product.setAttributeValue1(rs.getString("attribute_value1"));
            product.setAttributeValue10(rs.getString("attribute_value10"));
            product.setAttributeValue11(rs.getString("attribute_value11"));
            product.setAttributeValue12(rs.getString("attribute_value12"));
            product.setAttributeValue13(rs.getString("attribute_value13"));
            product.setAttributeValue14(rs.getString("attribute_value14"));
            product.setAttributeValue15(rs.getString("attribute_value15"));
            product.setAttributeValue16(rs.getString("attribute_value16"));
            product.setAttributeValue17(rs.getString("attribute_value17"));
            product.setAttributeValue18(rs.getString("attribute_value18"));
            product.setAttributeValue19(rs.getString("attribute_value19"));
            product.setAttributeValue2(rs.getString("attribute_value2"));
            product.setAttributeValue3(rs.getString("attribute_value3"));
            product.setAttributeValue4(rs.getString("attribute_value4"));
            product.setAttributeValue5(rs.getString("attribute_value5"));
            product.setAttributeValue6(rs.getString("attribute_value6"));
            product.setAttributeValue7(rs.getString("attribute_value7"));
            product.setAttributeValue8(rs.getString("attribute_value8"));
            product.setAttributeValue9(rs.getString("attribute_value9"));
            product.setCost(rs.getDouble("cost"));
            product.setFullName(rs.getString("full_name"));
            product.setHits(rs.getLong("hits"));
            product.setImage(rs.getString("image"));
            product.setIntroduction(rs.getString("introduction"));
            product.setGift(rs.getBoolean("is_gift"));
            product.setList(rs.getBoolean("is_list"));
            product.setMarketable(rs.getBoolean("is_marketable"));
            product.setTop(rs.getBoolean("is_top"));
            product.setKeyword(rs.getLong("keyword"));
            product.setMarketPrice(rs.getDouble("market_price"));
            product.setMemo(rs.getString("memo"));
            product.setMonthHits(rs.getLong("month_hits"));
            product.setMonthHitsDate(rs.getDate("month_hits_date"));
            product.setMonthSales(rs.getLong("month_sales"));
            product.setMonthSalesDate(rs.getDate("month_sales_date"));
            product.setName(rs.getString("name"));
            product.setPoint(rs.getLong("point"));
            product.setPrice(rs.getDouble("price"));
            product.setSales(rs.getLong("sales"));
            product.setScore(rs.getFloat("score"));
            product.setScoreCount(rs.getLong("score_count"));
            product.setSeoDescription(rs.getString("seo_description"));
            product.setSeoKeywords(rs.getString("seo_keywords"));
            product.setSeoTitle(rs.getString("seo_title"));
            product.setSn(rs.getString("sn"));
            product.setStock(rs.getInt("stock"));
            product.setStockMemo(rs.getString("stock_memo"));
            product.setTotalScore(rs.getLong("total_score"));
            product.setUnit(rs.getString("unit"));
            product.setWeekHits(rs.getLong("week_hits"));
            product.setWeekHitsDate(rs.getDate("week_hits_date"));
            product.setWeekSales(rs.getLong("week_sales"));
            product.setWeekSalesDate(rs.getDate("week_sales_date"));
            product.setWeight(rs.getInt("weight"));
            product.setBrand(rs.getLong("brand"));
            product.setGoods(rs.getLong("goods"));
            product.setProductCategory(rs.getLong("product_category"));
            return product;
        }
    };

    public List<Product> getMoreProducts(long last, int size) {
        List<Product> products = jdbcTemplate.query(
                "select * from (SELECT * FROM shopxx.xx_product order by id desc) p where p.id < ? limit ?",
                new Object[]{last, size},
                productRowMapper);

        return products;
    }

    public Product getProduct(long id) {
        Product product = jdbcTemplate.queryForObject(
                "SELECT * FROM shopxx.xx_product  p WHERE p.id = ?",
                new Object[]{id},
                productRowMapper);

        return product;
    }
}
